<?php
/**
 * 图片处理类 - JBlog
 * 
 * @copyright (c) 2008-2010 JBlog (www.lisijie.org)
 * @author lisijie <lisijie86@gmail.com>
 * @version $Id: Image.php 21 2012-07-12 05:08:19Z lisijie86@gmail.com $
*/

class Image {
	
	var $waterpic; //水印图片地址
	var $waterpos; //水印位置
	var $watertrans; //透明度
	
	function Image () {
		$this->__construct();
	}
	
	function __construct() {
		$this->waterpos = 6;
		$this->watertrans = 100;
	}
	
	//创建缩略图
	function thumb ($srcfile, $imgfile, $width, $height, $wm = false, $fix = false) {
		if ( !$width || !$height ) return false;
		if ( !function_exists('gd_info') ) return false;
		list($srcW, $srcH, $type) = getimagesize($srcfile);
		switch ( $type ) {
			case 1 :
				$src = imagecreatefromgif($srcfile); break;
			case 2 :
				$src = imagecreatefromjpeg($srcfile); break;
			case 3 :
				$src = imagecreatefrompng($srcfile); break;
			default:
				return false; break;
		}
		if ( !$fix ) {
			$scale = min($width/$srcW, $height/$srcH);
			$width = (int)$srcW * $scale;
			$height = (int)$srcH * $scale;
			$srcX = $srcY = 0;
		} else {
			if ($srcW/$srcH > $width/$height){
				$srcX = ($srcW-$srcH*$width/$height)/2;
				$srcY = 0;
				$srcW = $srcH*$width/$height;
			}else{
				$srcX = 0;
				$srcY = ($srcH-$srcW*$height/$width)/2;
				$srcH = $srcW*$height/$width;
			}			
		}
		if ( function_exists('imagecreatetruecolor') ) {
			$img = imagecreatetruecolor($width, $height);
			imagecopyresampled($img, $src, 0, 0, 0, 0, $width, $height, $srcW, $srcH);
		} else {
			$img = imagecreate($width, $height);
			imagecopyresized($img, $src, 0, 0, 0, 0, $width, $height, $srcW, $srcH);
		}
		if ( is_file($imgfile) ) {
			@unlink($imgfile);
		}
		$fileext = pathinfo($imgfile, PATHINFO_EXTENSION);
		switch ( $fileext ) {
			case 'gif' :
				imagegif($img, $imgfile); break;
			case 'png' :
				imagepng($img, $imgfile); break;
			default:
				imagejpeg($img, $imgfile); break;
		}
		imagedestroy($src);
		imagedestroy($img);
		if ( $wm ) {
			$this->watermark($imgfile);
		}
		return $imgfile;
	}
	
	//加水印
	function watermark ($srcfile) {
		if ( !is_file($this->waterpic) || !is_file($srcfile) ) {
			return false;
		}
		list($wmW,$wmH,$wmType) = getimagesize($this->waterpic);
		switch ( $wmType ) {
			case 1 : 
				$wm = imagecreatefromgif($this->waterpic); break;
			case 2 :
				$wm = imagecreatefromjpeg($this->waterpic); break;
			case 3 :
				$wm = imagecreatefrompng($this->waterpic); break;
		}
		if ( !$wm ) {
			return false;
		}
		list($srcW, $srcH, $srcType) = getimagesize($srcfile);
		if ( $wmW > $srcW || $wmH > $srcH ) return false;
		switch ( $srcType ) {
			case 1 : 
				$src = imagecreatefromgif($srcfile); break;
			case 2 :
				$src = imagecreatefromjpeg($srcfile); break;
			case 3 :
				$src = imagecreatefrompng($srcfile); break;
		} 
		if ( !$src ) {
			return false;
		}
		//水印位置
		switch ($this->waterpos) {				
			case 1 :  //顶部居左
				$srcX = 0; $srcY = 0; break;				
			case 2 :  //顶部居中
				$srcX = ($srcW - $wmW) / 2; $srcY = 0; break;				
			case 3 :  //顶部居右
				$srcX = $srcW; $srcY = 0; break;				
			case 4 :  //底部居左
				$srcX = 0; $srcY = $srcH; break;				
			case 5 :  //底部居中
				$srcX = ($srcW - $wmW) / 2; $srcY = $srcH; break;		
			case 6 :  //底部居右
				$srcX = $srcW-$wmW; $srcY = $srcH-$wmH; break;
			default : //随机
				$srcX = mt_rand(0,$srcW-$wmW); $srcY = mt_rand(0,$srcH-$wmH);
		}
		if ( function_exists('ImageAlphaBlending') ) {
			imagealphablending($wm, true);
			imagesavealpha($wm, true);
		}
		imagecopymerge($src, $wm, $srcX, $srcY, 0, 0, $wmW, $wmH, $this->watertrans);
		switch ( $srcType ) {
			case 1 :
				imagegif($src, $srcfile); break;
			case 2 :
				imagejpeg($src, $srcfile); break;
			case 3 : 
				imagepng($src, $srcfile); break;
			default :
				imagejpeg($src, $srcfile);
		}
		imagedestroy($src);
		imagedestroy($wm);
		return true;
	}
}
